﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elderos.Highlight.Search;
using Elderos.Ontology;

#pragma warning disable 1998
namespace Elderos.Highlight.Rules
{
    public abstract class SynonymRule : RankingRule
    {
        protected SynonymRule(IOntologyData ontology) : base(ontology) {}
        protected override async Task<Dictionary<TextEntity, IRuleResult>> InnerRank(HighlightResult highlightResult)
        {
            var results = new Dictionary<TextEntity, IRuleResult>();

            foreach (var position in highlightResult.Positions.SelectMany(x => x.SubpositionsFlattened))
            {
                var syno = highlightResult.Text.Substring(position.CharInterval.First, position.CharInterval.Length);
                IRuleResult result = ComputeResult(syno);
                
                foreach (var entity in position.Entities)
                {
                    results.Add(entity, result);
                }
            }

            return results;
        }

        protected abstract IRuleResult ComputeResult(string synonym);
    }
}
#pragma warning restore 1998